<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/17
 * Time: 10:17
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Car extends Base
{
    /**
     * 车辆列表展示
     * @return mixed|\think\response\Json
     */
    public function carIndex(){
        if(request()->isAjax()){
            $car_name = input('car_name'); // 名称
            $car_is_tuijian = input('car_is_tuijian'); // 状态
            $car_cate = input('car_cate'); // 分类
            $where['car_del'] = 1;
            if(isset($car_is_tuijian) && $car_is_tuijian != ""){
                $where['car_is_tuijian'] = $car_is_tuijian;
            }
            if(isset($car_cate) && $car_cate != ''){
                $where['car_cate_id'] = $car_cate;
            }
            if(isset($car_name) && $car_name != ''){
                $where['car_name'] = ['like', '%' . $car_name . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('car')
                ->join('up_car_cate', 'cc_id = car_cate_id', 'left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('car')
                ->join('up_car_cate', 'cc_id = car_cate_id', 'left')
                ->where($where)
                ->field('car_id, car_thumb, car_name, car_price, car_is_tuijian, car_news, cc_name')
                ->order('car_time desc')
                ->page($NowPage, $limits)
                ->select();
            foreach($lists as $k => $v){
                if(!empty($v['car_news'])){
                    $car_news = explode(',', $v['car_news']);
                    $lists[$k]['news_num'] = count($car_news);
                }else{
                    $lists[$k]['news_num'] = 0;
                }
                
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cc = Db::table('up_car_cate')->field('cc_id, cc_name')->select();
        $this->assign('cc', $cc);
        return $this->fetch('car/carindex');
    }
    
    /**
     * 添加车辆
     * @return mixed|\think\response\Json
     */
    public function carAdd(){
        if(Request::instance()->isPost()){
            $car_thumb = input('car_thumb');
            $car_images = input('photo');
            $car_out_images = input('photo1');
            $car_middle_images = input('photo3');
            $car_chair_images = input('photo4');
            $car_detail_images = input('photo5');
            $car_special_images = input('photo6');
            $car_long_images = input('photo7');
            if(empty($car_thumb)){
                return json(['code' => 100, 'msg' => '请上传缩略图']);
            }
            if(empty($car_images)){
                return json(['code' => 100, 'msg' => '请上传车辆图片']);
            }
            if(empty($car_out_images)){
                return json(['code' => 100, 'msg' => '请上传车辆外观图片']);
            }
            if(empty($car_middle_images)){
                return json(['code' => 100, 'msg' => '请上传车辆中控图片']);
            }
            if(empty($car_chair_images)){
                return json(['code' => 100, 'msg' => '请上传车辆座椅图片']);
            }
            if(empty($car_detail_images)){
                return json(['code' => 100, 'msg' => '请上传车辆细节图片']);
            }
            if(empty($car_special_images)){
                return json(['code' => 100, 'msg' => '请上传车辆特点图片']);
            }
//            if(empty($car_long_images)){
//                return json(['code' => 100, 'msg' => '请上传车辆长图']);
//            }
            $insert = [
                'car_name' => trim(input('car_name'), ' '),
                'car_cate_id' => input('car_cate_id'),
                'car_thumb' => $car_thumb,
                'car_images' => $car_images,
                'car_price' => trim(input('car_price'), ' '),
                'car_out_images' => $car_out_images,
                'car_middle_images' => $car_middle_images,
                'car_chair_images' => $car_chair_images,
                'car_detail_images' => $car_detail_images,
                'car_special_images' => $car_special_images,
                'car_long_images' => $car_long_images,
                'car_detail' => input('car_detail'),
                'car_news' => input('car_news'),
                'car_time' => date('Y-m-d H:i:s'),
                'car_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_car')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $new = Db::table('up_new')->where('new_status',1)->field('new_id, new_title')->order('new_time desc')->select();
        $cc = Db::table('up_car_cate')->field('cc_id, cc_name')->select();
        $this->assign('cc', $cc);
        $this->assign('new', $new);
        return $this->fetch('car/caradd');
    }
    
    /**
     * 车辆详情
     * @return mixed
     */
    public function carDetail(){
        $car_id = input('car_id');
        $detail = Db::table('up_car')
            ->join('up_car_cate', 'car_cate_id = cc_id', 'left')
            ->field('car_thumb, car_images, car_name, car_price, car_detail, car_is_tuijian, 
            car_out_images, car_special_images, car_middle_images, car_chair_images, car_detail_images, 
            cc_name, car_long_images')
            ->where('car_id', $car_id)
            ->find();
        $detail['car_images'] = explode(',', trim($detail['car_images'], ','));
        $detail['car_out_images'] = explode(',', trim($detail['car_out_images'], ','));
        $detail['car_special_images'] = explode(',', trim($detail['car_special_images'], ','));
        $detail['car_middle_images'] = explode(',', trim($detail['car_middle_images'], ','));
        $detail['car_chair_images'] = explode(',', trim($detail['car_chair_images'], ','));
        $detail['car_detail_images'] = explode(',', trim($detail['car_detail_images'], ','));
        $detail['car_long_images'] = explode(',', trim($detail['car_long_images'], ','));
        $this->assign('car', $detail);
        return $this->fetch('car/cardetail');
    }
    
    /**
     * 编辑车辆
     * @return mixed|\think\response\Json
     */
    public function carEdit(){
        $car_id = input('car_id');
        if(Request::instance()->isPost()){
            $car_thumb = input('img1');
            $car_images = input('photo');
            $car_out_images = input('photo3');
            $car_middle_images = input('photo4');
            $car_chair_images = input('photo5');
            $car_detail_images = input('photo6');
            $car_special_images = input('photo7');
            $car_long_images = input('photo8');
            if(empty($car_thumb)){
                return json(['code' => 100, 'msg' => '请上传缩略图']);
            }
            if(empty($car_images)){
                return json(['code' => 100, 'msg' => '请上传车辆图片']);
            }
            if(empty($car_out_images)){
                return json(['code' => 100, 'msg' => '请上传车辆外观图片']);
            }
            if(empty($car_middle_images)){
                return json(['code' => 100, 'msg' => '请上传车辆中控图片']);
            }
            if(empty($car_chair_images)){
                return json(['code' => 100, 'msg' => '请上传车辆座椅图片']);
            }
            if(empty($car_detail_images)){
                return json(['code' => 100, 'msg' => '请上传车辆细节图片']);
            }
            if(empty($car_special_images)){
                return json(['code' => 100, 'msg' => '请上传车辆特点图片']);
            }
//            if(empty($car_long_images)){
//                return json(['code' => 100, 'msg' => '请上传车辆长图']);
//            }
            $update = [
                'car_name' => trim(input('car_name'), ' '),
                'car_cate_id' => input('car_cate_id'),
                'car_thumb' => $car_thumb,
                'car_images' => $car_images,
                'car_price' => trim(input('car_price'), ' '),
                'car_news' => input('car_news'),
                'car_out_images' => $car_out_images,
                'car_middle_images' => $car_middle_images,
                'car_chair_images' => $car_chair_images,
                'car_detail_images' => $car_detail_images,
                'car_special_images' => $car_special_images,
//                'car_long_images' => $car_long_images,
                'car_detail' => input('car_detail'),
                'car_updatetime' => date('Y-m-d H:i:s')
            ];
            if($car_long_images) $update['car_long_images'] = $car_long_images;
            $res = Db::table('up_car')->where('car_id', $car_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $detail = Db::table('up_car')
            ->field('car_thumb, car_images, car_name, car_price, car_detail, car_is_tuijian, car_news, 
            car_out_images, car_special_images, car_middle_images, car_chair_images, car_detail_images, 
            car_cate_id, car_long_images')
            ->where('car_id', $car_id)
            ->find();
        $new = Db::table('up_new')->where('new_status',1)->field('new_id, new_title')->order('new_time desc')->select();
        $cc = Db::table('up_car_cate')->field('cc_id, cc_name')->select();
        $this->assign([
            'car_id' => $car_id,
            'car' => $detail,
            'new' => $new,
            'cc' => $cc
        ]);
        return $this->fetch('car/caredit');
    }
    
    /**
     * 是否推荐
     * @return \think\response\Json
     */
    public function carStatus(){
        $car_id = input('id');
        $tuijian = Db::table('up_car')->where('car_id', $car_id)->value('car_is_tuijian');
        if($tuijian == 1){
            $status = 2;
        }else{
            $count = Db::table('up_car')->where('car_is_tuijian', 1)->where('car_del', 1)->count();
            if($count >= 3){
                return json(['code' => 100, 'msg' => '最多推荐3个车型，请先取消其他车型']);
            }
            $status = 1;
        }
        $res = Db::table('up_car')->where('car_id', $car_id)->setField('car_is_tuijian', $status);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除车辆
     * @return \think\response\Json
     */
    public function carDel(){
        $car_id = input('id');
        Db::startTrans();
        try{
            // 删除车辆
            Db::table('up_car')->where('car_id', $car_id)->setField('car_del', 2);
            // 删除类别
            Db::table('up_car_type_param')->where('ctp_car_id', $car_id)->delete();
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 参数列表展示
     * @return mixed|\think\response\Json
     */
    public function typeIndex(){
        $car_id = input('car_id');
        if($car_id != ''){
            session('car_id', $car_id);
        }
        if(request()->isAjax()){
            $where['ctp_car_id'] = session('car_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('car_type_param')
                ->join('up_car_type', 'c_type_id = ctp_type_id', 'left')
                ->where($where)
                ->group('ctp_type_id')
                ->count();// 获取总条数
            $lists = Db::name('car_type_param')
                ->join('up_car_type', 'c_type_id = ctp_type_id', 'left')
                ->where($where)
                ->field('ctp_id, ctp_car_id, ctp_type_id, c_type_name')
                ->order('ctp_id desc')
                ->group('ctp_type_id')
                ->page($NowPage, $limits)
                ->select();
            foreach($lists as $k => $v){
                $c_param_id = Db::table('up_car_type_param')
                    ->where('ctp_car_id', session('car_id'))
                    ->where('ctp_type_id', $v['ctp_type_id'])
                    ->column('ctp_param_id');
                $c_param_name = Db::table('up_car_param')->where('c_param_id', 'IN', $c_param_id)->column('c_param_name');
                $lists[$k]['c_param_name'] = implode(',', $c_param_name);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('car/typeindex');
    }
    
    /**
     * 添加参数
     * @return mixed|\think\response\Json
     */
    public function typeAdd(){
        if(Request::instance()->isPost()){
            $ctp_type_id = input('car_type_id');
            $ctp_param_id = input('car_param_id');
            if(empty($ctp_type_id)){
                return json(['code' => 100, 'msg' => '请选择类别']);
            }
            if(empty($ctp_param_id)){
                return json(['code' => 100, 'msg' => '请选择属性']);
            }
            $ctp_param_id = explode(',', trim($ctp_param_id, ','));
            $all_param = Db::table('up_car_type_param')
                ->where('ctp_car_id', session('car_id'))
                ->where('ctp_type_id', $ctp_type_id)
                ->column('ctp_param_id');
            
            //=====去重
            if(!empty($all_param)){
                // 共同的部分
                $same = array_intersect($ctp_param_id, $all_param);
                // 需要插入的数组
                $new = array_diff($ctp_param_id, $same);
            }else{
                $new = $ctp_param_id;
            }
            
            if(!empty($new)){
                $insert = [];
                foreach($new as $k => $v){
                    $insert[] = [
                        'ctp_car_id' => session('car_id'),
                        'ctp_type_id' => $ctp_type_id,
                        'ctp_param_id' => $v
                    ];
                }
                $res = Db::table('up_car_type_param')->insertAll($insert);
                if($res){
                    return json(['code' => 200, 'msg' => '添加成功']);
                }else{
                    return json(['code' => 100, 'msg' => '添加失败']);
                }
            }else{
                return json(['code' => 100, 'msg' => '重复添加']);
            }
        }
        // 类别
        $type = Db::table('up_car_type')
            ->field('c_type_id, c_type_name')
            ->where('c_type_show', 1)
            ->order('c_type_time desc')
            ->select();
        $this->assign('car_type', $type);
        return $this->fetch('car/typeadd');
    }
    
    /**
     * 编辑类别
     * @return mixed|\think\response\Json
     */
    public function typeEdit(){
        if(Request::instance()->isPost()){
            $old_type_id = input('old_type_id');
            $new_type_id = input('ctp_type_id');
            $ctp_param_id = input('ctp_param_id');
            if(empty($new_type_id)){
                return json(['code' => 100, 'msg' => '请选择类别']);
            }
            if(empty($ctp_param_id)){
                return json(['code' => 100, 'msg' => '请选择属性']);
            }
            $ctp_param_id = explode(',', trim($ctp_param_id, ','));
            // 如果修改后的类别和被修改的类别相同
            if($old_type_id == $new_type_id){
                $change = $old_type_id;
            }else{
                $change = $new_type_id;
            }
            Db::table('up_car_type_param')
                ->where('ctp_car_id', session('car_id'))
                ->where('ctp_type_id', $change)
                ->delete();
            $insert = [];
            foreach($ctp_param_id as $k => $v){
                $insert[] = [
                    'ctp_car_id' => session('car_id'),
                    'ctp_type_id' => $change,
                    'ctp_param_id' => $v
                ];
            }
            $res = Db::table('up_car_type_param')->insertAll($insert);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $ctp_type_id = input('ctp_type_id');
        // 类别
        $car_type = Db::table('up_car_type')
            ->field('c_type_id, c_type_name')
            ->where('c_type_show', 1)
            ->order('c_type_time desc')
            ->select();
        // 属性
        $car_param = Db::table('up_car_param')
            ->field('c_param_id, c_param_name')
            ->where('c_param_fid', $ctp_type_id)
            ->where('c_param_show', 1)
            ->order('c_param_time desc')
            ->select();
        // 回显
        $info_type = Db::table('up_car_type_param')
            ->field('ctp_car_id, ctp_type_id')
            ->where('ctp_car_id', session('car_id'))
            ->where('ctp_type_id', $ctp_type_id)
            ->find();
        $info_param = Db::table('up_car_type_param')
            ->where('ctp_car_id', session('car_id'))
            ->where('ctp_type_id', $ctp_type_id)
            ->column('ctp_param_id');
        $this->assign([
            'car_type' => $car_type,
            'car_param' => $car_param,
            'ctp_type_id' => $ctp_type_id,
            'info_type' => $info_type,
            'info_param' => implode(',', $info_param)
        ]);
        return $this->fetch('car/typeedit');
    }
    
    /**
     * 删除类别
     * @return \think\response\Json
     */
    public function typeDel(){
        $ctp_type_id = input('id');
        $res = Db::table('up_car_type_param')
            ->where('ctp_car_id', session('car_id'))
            ->where('ctp_type_id', $ctp_type_id)
            ->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
           
}